package com.jiantong.lawEnforcing.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.lawEnforcing.impl.dao.PartiesInformationDao;
import com.jiantong.lawEnforcing.model.PartiesInformation;
import com.jiantong.lawEnforcing.service.PartiesInformationService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *  当事人信息
 * Created by mz03 on 2017/8/14.
 */
@Slf4j
@Service
@RpcProvider
public class PartiesInformationServiceImpl implements PartiesInformationService {

    private final PartiesInformationDao partiesInformationDao;

    @Autowired
    public PartiesInformationServiceImpl(PartiesInformationDao partiesInformationDao) {
        this.partiesInformationDao = partiesInformationDao;
    }

    /**
     * id 查找
     *
     * @param id
     * @return partiesInformation
     */
    @Override
    public Response<PartiesInformation> finById(Long id) {
        try {
            PartiesInformation partiesInformation = partiesInformationDao.findById(id);
            if (partiesInformation != null) {
                return Response.ok(partiesInformation);
            } else {
                log.error("partiesInformation (id={}) is not found", id);
                return Response.fail("partiesInformation.not.found");
            }
        }catch (Exception e){
            log.error("failed to find partiesInformation(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("partiesInformation.find.fail");
        }
    }

    /**
     * 创建
     *
     * @param partiesInformation 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Long> create(PartiesInformation partiesInformation) {
        try {
            partiesInformationDao.create(partiesInformation);
            return Response.ok(partiesInformation.getId());
        }catch (Exception e) {
            log.error("failed.to.create.partiesInformation:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.partiesInformation.failed");
        }
    }

    /**
     * 更新
     *
     * @param partiesInformation 单据对象
     * @return true or false
     */
    @Override
    public Response<Boolean> update(PartiesInformation partiesInformation) {
        try{
            return Response.ok(partiesInformationDao.update(partiesInformation));
        }catch (Exception e){
            log.error("failed.to.update.partiesInformation:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("update.partiesInformation.failed");
        }
    }
}
